package com.lottery.game.websocket;


import com.lottery.game.common.constants.GameConstants;
import com.lottery.game.common.utils.MapUtils;
import com.lottery.game.common.utils.UUIDUtils;
import com.lottery.game.core.model.dto.UserToken;
import com.lottery.game.core.modules.service.app.v2.PassportService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.Map;

public class MyHandshakeInterceptor extends HttpSessionHandshakeInterceptor {

	@Autowired
	private PassportService passportService;

    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
            ServerHttpResponse response, WebSocketHandler wsHandler,
            Map<String, Object> attributes) throws Exception {
    try {

    	if (request instanceof ServletServerHttpRequest) {
			ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;

			//chatType =0 房间群聊
			String chatType =  servletRequest.getServletRequest().getParameter("chatType");
			String ticket =  servletRequest.getServletRequest().getHeader("ticket");
            String userId = null;
            if(StringUtils.isEmpty(ticket)) {
							ticket = servletRequest.getServletRequest().getParameter("ticket");
            }
            if(!StringUtils.isEmpty(ticket)) {
            	//TODO
							UserToken userToken = MapUtils.mapToObject(passportService.checkToken(ticket), UserToken.class);
                userId = userToken.getId();
            }else {
                userId = servletRequest.getServletRequest().getParameter("userId");
            }



            //通用 ，所有websocket建立连接至少需要 chatType
			attributes.put("chatType",chatType );
			if(GameConstants.CHATTYPE_GROUPCHAT.equals(chatType)) {
				//连入websocket时传入chartId 群聊传房间号和群聊标记。 客服聊天传入 用户id客服聊天类别
				if(StringUtils.isEmpty(userId)) {
					userId = UUIDUtils.randomUUID();
				}
				attributes.put("userId",userId);
				String roomId = servletRequest.getServletRequest().getParameter("roomNumber");
				attributes.put("roomId",roomId );
				attributes.put("chartId",roomId );
			}else if(GameConstants.CHATTYPE_CUSSERVICECHAT.equals(chatType)||GameConstants.CHATTYPE_CUSREPLY.equals(chatType)){
				String chartId = servletRequest.getServletRequest().getParameter("chartId");
				if(chartId==null) {
				    chartId = userId;
				}
				attributes.put("chartId",chartId);
				attributes.put("userId",userId);
			}else if(GameConstants.CHATTYPE_SYSTEM_MSG.equals(chatType)) {
			    attributes.put("userId",userId);
			    //需要其他参数，写这里
			    attributes.put("chartId",GameConstants.SYS_PARAM_CHATID);
			}else if(GameConstants.SYS_OVERALL.equals(chatType)) { //全局聊天
				String randomUserId=UUIDUtils.randomUUID();
				attributes.put("randomUserId",randomUserId);
				attributes.put("chartId",GameConstants.SYS_OVERALL);
//            } else if (GameConstants.SYS_MSG_SXFEN_COUNT.equals(chatType) || GameConstants.SYS_MSG_CUSSERVICECHAT_NOTICE.equals(chatType)) {
//                //上下分消息传递 //客服消息通知传递
//                attributes.put("userId", userId);
//                attributes.put("chartId", chatType);
            } else if (GameConstants.CHAT_TYPE_ROOM_MSG.equals(chatType)){
				if(StringUtils.isEmpty(userId)) {
					userId = UUIDUtils.randomUUID();
				}
				String roomId = servletRequest.getServletRequest().getParameter("roomNumber");
				attributes.put("userId",userId);
				attributes.put("roomId",roomId );
				attributes.put("chartId","roomMsg|"+roomId );
			}
    	}

    	} catch (Exception e) {
		e.printStackTrace();
	}
        return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    @Override
    public void afterHandshake(ServerHttpRequest request,
            ServerHttpResponse response, WebSocketHandler wsHandler,
            Exception ex) {
        super.afterHandshake(request, response, wsHandler, ex);
    }
}
